package com.itrip.itripauth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itrip.itripauth.entity.HotelRoom;
import com.itrip.itripauth.entity.Image;
import com.itrip.itripauth.entity.LabelDic;
import com.itrip.itripauth.service.HotelRoomService;
import com.itrip.itripauth.service.ImageService;
import com.itrip.itripauth.service.LabelDicService;
import com.itrip.itripauth.util.Dto;
import com.itrip.itripauth.util.DtoUtil;
import com.itrip.itripauth.vo.DateUtil;
import com.itrip.itripauth.vo.EmptyUtils;
import com.itrip.itripauth.vo.HotelRoomVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 酒店房间表 前端控制器
 * </p>
 *
 * @author 四阶段项目二组
 * @since 2020-02-25
 */
@RestController
@RequestMapping("/biz/api/hotelroom")
public class HotelRoomController {
    @Autowired
    private  HotelRoomService hotelRoomService;
    @Autowired
    private ImageService imageService;
    @Autowired
    private LabelDicService labelDicService;
    @ApiOperation("查询房间的类型")
    @GetMapping("/queryhotelroombed")
    public Dto queryhotelroombed(){
        QueryWrapper<LabelDic> queryWrapper=new QueryWrapper<LabelDic>();
        queryWrapper.eq("parent_id",1);
       return DtoUtil.returnDataSuccess(labelDicService.list(queryWrapper));
    }
    @ApiOperation("查询酒店房间")
    @PostMapping("/queryhotelroombyhotel")
    public Dto queryhotelroombyhotel(
            @ApiParam(name = "vo",required = true)
            @RequestBody HotelRoomVo vo
    ){
        List<List<HotelRoom>> hotelRoomVOLists = null;
        try {
            Map<String, Object> param = new HashMap();
            if (EmptyUtils.isEmpty(vo.getHotelId())) {
                return DtoUtil.returnFail("酒店ID不能为空");
            }
            if (EmptyUtils.isEmpty(vo.getStartDate()) || EmptyUtils.isEmpty(vo.getEndDate())) {
                return DtoUtil.returnFail("必须填写酒店入住及退房时间");
            }
            if (EmptyUtils.isNotEmpty(vo.getStartDate()) && EmptyUtils.isNotEmpty(vo.getEndDate())) {
                if (vo.getStartDate().getTime() > vo.getEndDate().getTime()) {
                    return DtoUtil.returnFail("入住时间不能大于退房时间");
                }
                List<Date> dates = DateUtil.getBetweenDates(vo.getStartDate(), vo.getEndDate());
                param.put("timesList", dates);
            }
            if (vo.getHotelId()!=null){
                param.put("hotelId", vo.getHotelId());
            }
            if (vo.getIsBook()!=null){
                param.put("isBook", vo.getIsBook());
            }
            if (vo.getIsHavingBreakfast()!=null){
                param.put("isHavingBreakfast", vo.getIsHavingBreakfast());
            }
            if (vo.getIsTimelyResponse()!=null){
                param.put("isTimelyResponse", vo.getIsTimelyResponse());
            }
            if (vo.getRoomBedTypeId()!=null){
                param.put("roomBedTypeId", vo.getRoomBedTypeId());
            }
            if (vo.getIsCancel()!=null){
                param.put("isCancel", vo.getIsCancel());
            }
            if (vo.getPayType()!=null){
                param.put("payType", vo.getPayType());
            }

            List<HotelRoom> originalRoomList = hotelRoomService.hotelRoomListsByMap(param);
            hotelRoomVOLists = new ArrayList();
            for (HotelRoom roomVO : originalRoomList) {
                List<HotelRoom> tempList = new ArrayList<HotelRoom>();
                tempList.add(roomVO);
                hotelRoomVOLists.add(tempList);
            }
            return DtoUtil.returnSuccess("查询酒店房型列表成功成功", hotelRoomVOLists);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询酒店房型列表失败");
        }
    }

    @ApiOperation("根据target_id查询酒店房型图片")
    @GetMapping("getimg/{targetId}")
    public Dto getimg(
            @ApiParam(name = "targetId",required = true)
            @PathVariable Integer targetId
    ){
        QueryWrapper<Image> queryWrapper=new QueryWrapper<Image>();
        queryWrapper.eq("type",1);
        queryWrapper.eq("target_id",targetId);
        return  DtoUtil.returnDataSuccess(imageService.list(queryWrapper));
    }


}
